from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.kbd import Button, Cancel, Checkbox, Next

from src.application.order.enums import OrderPeriod
from src.presentation.tg_bot.dialogs.client.orders.authorization_type.handlers import start_authorization_change
from src.presentation.tg_bot.dialogs.client.orders.extend_order.dialogs import (
    extend_confirmation_window,
    extend_order_window,
)
from src.presentation.tg_bot.dialogs.client.orders.proxy_details import show_ordered_proxies
from src.presentation.tg_bot.dialogs.widgets import LocaleText

# from src.presentation.tg_bot.dialogs.getters.orders import order_info_getter
from src.presentation.tg_bot.states import OrderDetailsSG

from ..rotation_period.handlers import start_rotation_period_change
from .handlers import change_autorenew, export_csv, send_qr_code, send_vpn_config

order_details_window = Window(
    LocaleText("order-overview", when=F["order_type"] == "proxy"),
    LocaleText("subscription-overview", when=F["order_type"] == "subcription"),
    LocaleText("order_id", order_id="{order_id}", when=F["order_type"] != "subcription"),
    LocaleText("status", status="{status}"),
    LocaleText(
        "proxies-in-order",
        proxies_amount="{proxies_amount}",
        when=F["order_type"] == "proxy",
    ),
    LocaleText("package-price", package_price="{package_price}$"),
    LocaleText("complete-price", complete_price="{complete_price}$"),
    LocaleText(
        "one-proxy-price",
        one_proxy_price="{one_proxy_price}$",
        when=(F["one_proxy_price"]) & (F["order_type"] == "proxy"),
    ),
    LocaleText(
        "proxy-type",
        proxy_type="{proxy_type_translated}",
        when=F["proxy_type_translated"],
    ),
    LocaleText(
        "proxy-privacy",
        proxy_privacy="{proxy_privacy_translated}",
        when=F["proxy_privacy_translated"],
    ),
    LocaleText("rotation-type", rotation_type="{rotation_type}", when=F["rotation_type"]),
    LocaleText(
        "rotation-period",
        rotation_period="{rotation_period}",
        when=F["rotation_period"],
    ),
    LocaleText(
        "authorization-type",
        authorization_type="{authorization_type}",
        when=F["authorization_type"],
    ),
    LocaleText(
        "authorization-ip",
        authorization_ip="{authorization_ip}",
        when=F["authorization_ip"],
    ),
    LocaleText("proxy-country", country="{country}", when=F["country"]),
    LocaleText("proxy-category", category="{category}", when=F["proxy_privacy"] == "shared"),
    LocaleText("proxy-provider", provider="{provider}", when=F["provider"]),
    LocaleText("proxy-site", site="{site}", when=F["proxy_privacy"] == "shared"),
    LocaleText(
        "authorization-not-set",
        when=(~F["authorization_ip"]) & (F["proxy_privacy"] == "shared") & (F["order_status"] != "archive"),
    ),
    LocaleText("created-at", created_at="{created_at}"),
    LocaleText("selected-period", period="{order_period}", when=F["order_period"]),
    LocaleText(
        "expiring-on",
        expiring_on="{expiring_on}",
        when=(F["order_type"] != "subscription") & (F["expiring_on"]),
    ),
    LocaleText(
        "subscribed-until",
        subscribed_until="{expiring_on}",
        when=F["order_type"] == "subscription",
    ),
    LocaleText("server-time", server_time="{server_time}", when=F["expiring_on"]),
    Button(
        LocaleText("export-to-csv"),
        id="export_csv",
        on_click=export_csv,
        when=(F["proxies_amount"] > 0) & (F["order_status"] != "archive"),
    ),
    Button(
        LocaleText("send-qr-code-btn"),
        id="generate_qr_code",
        on_click=send_qr_code,
        when=(F["order_type"] == "vpn") & (F["order_status"] != "archive"),
    ),
    Button(
        LocaleText("send-config-btn"),
        id="send_vpn_config",
        on_click=send_vpn_config,
        when=(F["order_type"] == "vpn") & (F["order_status"] != "archive"),
    ),
    Button(
        LocaleText("show-ordered-proxies"),
        id="show_ordered_proxies",
        on_click=show_ordered_proxies,
        when=(F["proxies_amount"] > 0) & (F["order_status"] != "archive"),
    ),
    Next(
        LocaleText("extend-order"),
        when=(F["order_status"] != "archive") & (F["order_type"] != "subscription") & (F["order_type"] != "vpn_router"),
    ),
    Next(
        LocaleText("extend-subscription"),
        when=(F["order_status"] != "archive") & (F["order_type"] == "subscription"),
    ),
    Button(
        LocaleText("change-authorization-type"),
        id="start_authorization_type",
        on_click=start_authorization_change,
        when=(F["order_status"] != "archive") & (F["proxies_amount"] > 0),
    ),
    Button(
        LocaleText("change-rotation-time"),
        id="change_rotation_time",
        on_click=start_rotation_period_change,
        when=(F["rotation_type"]) & (F["order_status"] != "archive") & (F["proxies_amount"] > 0),
    ),
    Checkbox(
        LocaleText("autorenew-on"),
        LocaleText("autorenew-off"),
        id="autorenew",
        on_state_changed=change_autorenew,
        when=(F["period"] != OrderPeriod.four_hours)
        & (F["order_status"] != "archive")
        & (F["order_type"] != "vpn_router"),
    ),
    Cancel(LocaleText("back-btn")),
    state=OrderDetailsSG.order_details,
    # getter=order_info_getter,
)

order_details_dialog = Dialog(
    order_details_window,
    extend_order_window,
    extend_confirmation_window,
)
